package com.bjsxt.user.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bjsxt.commom.Util.PageInfoUtil;
import com.bjsxt.commom.dao.impl.BasicDaoImpl;
import com.bjsxt.user.dao.IAUserDao;
import com.bjsxt.user.pojo.AUser;

@Repository("userDao")
public class AUserDaoImpl extends BasicDaoImpl<AUser>  implements IAUserDao 
{
	/**
	 * 为父类的方法拼接条件
	 */
	@Override
	public AUser findOne(String type, Map<String, Object> sourceMap)
	{
		Map<String,Object> condMap  = new HashMap<String,Object>();
		StringBuffer hqlsb = new StringBuffer();
		hqlsb.append(" from AUser au where 1 = 1 ");
		if(!"".equalsIgnoreCase(sourceMap.get("id")+"") && sourceMap.get("id")!= null)
		{
			hqlsb.append(" and id = :id  ");
			condMap.put("id", Byte.valueOf(sourceMap.get("id")+""));
		}else if(!"".equalsIgnoreCase(sourceMap.get("password")+"") && sourceMap.get("password")!= null 
				&& "".equalsIgnoreCase(sourceMap.get("email")+"") && sourceMap.get("email")!= null)
		{
			hqlsb.append(" and email = :email and password = :password ");
			condMap.put("email", sourceMap.get("email"));
			condMap.put("password", sourceMap.get("password"));
		}else
		{
			return null;
		}
		hqlsb.append(" order by au.createTime desc");
		return this.findOneDao(hqlsb.toString(), condMap);
	}

	@Override
	public List<AUser> findList(PageInfoUtil pageInfoUtil,String type, Map<String, Object> sourceMap)
	{
		Map<String,Object> condMap = new HashMap<String,Object>();
		/* 拼接hql语句 */
		StringBuffer hqlsb = new StringBuffer();
		hqlsb.append("from AUser au where 1 =1 ");
		if(!"".equalsIgnoreCase(sourceMap.get("keyword")+"") && sourceMap.get("keyword")!=null)
		{
			hqlsb.append(" and ((name like :keyword) or (email like :keyword)) ");
			condMap.put("keyword","%"+sourceMap.get("keyword")+"%");
		}
		if(!"".equalsIgnoreCase(sourceMap.get("status")+"") && sourceMap.get("status")!=null)
		{
			hqlsb.append(" and status = :status");
			condMap.put("status", Byte.valueOf(sourceMap.get("status")+""));
		}
		if(sourceMap.get("startTime") instanceof Date && sourceMap.get("endTime") instanceof Date)
		{
			hqlsb.append(" and createTime >= :startTime and createTime < :endTime");
			condMap.put("startTime", sourceMap.get("startTime"));
			condMap.put("endTime", sourceMap.get("endTime"));
		}
		hqlsb.append(" order by au.createTime desc");
		if(pageInfoUtil == null)
		{
			/* 不分页 */
		 	return  this.findListDao(hqlsb.toString(), condMap);
		}
		return this.findPageListDao(pageInfoUtil, hqlsb.toString(),condMap);
	}
}
